package com.test.deencredit.dialog_deencredit;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.test.deencredit.R;
import com.test.deencredit.bean_deencredit.config_deencredit.AppShowCustomize_deencredit;
import com.test.deencredit.bean_deencredit.userinfo_deencredit.AccountList_Fr_deencredit;
import com.test.deencredit.bean_deencredit.userinfo_deencredit.BankAccount_Fr_deencredit;
import com.test.deencredit.util_deencredit.http_deencredit.HttpUtil_deencredit;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;


public class SubmitAccountDialog_Fr_deencredit extends Dialog {

    private LinearLayout groupLayout_deencredit;

    private final Runnable submitRunnable_deencredit;

    private final String page_deencredit;

    public SubmitAccountDialog_Fr_deencredit(@NonNull Context context, Runnable submitRunnable, String page) {
        super(context);
        this.submitRunnable_deencredit = submitRunnable;
        this.page_deencredit = page;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_submit_account_fr_deencredit);

        getWindow().setBackgroundDrawableResource(R.color.transparent);
        setCanceledOnTouchOutside(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        initView_deencredit();
    }

    @SuppressLint("SetTextI18n")
    private void initView_deencredit(){

        groupLayout_deencredit = findViewById(R.id.dsaf_layout_group);

        findViewById(R.id.dsaf_ib_close).setOnClickListener(v -> {
            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"SUBMIT_CANCEL_CLICK","");
            dismiss();
        });

        findViewById(R.id.dsaf_btn_submit).setOnClickListener(v -> {
            HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit,"SUBMIT_CONFIRM_CLICK","");
            if (submitRunnable_deencredit !=null) submitRunnable_deencredit.run();
            dismiss();
        });

        refreshAccountList_deencredit();

    }

    private void refreshAccountList_deencredit(){
        groupLayout_deencredit.removeAllViews();

        ArrayList<BankAccount_Fr_deencredit> accounts = AccountList_Fr_deencredit.getInstance_deencredit().getBankAccountList_deencredit();

        for (BankAccount_Fr_deencredit account:accounts){
            addAccountView_deencredit(account);
        }

    }

    private void addAccountView_deencredit(BankAccount_Fr_deencredit account){

        if (account == null) return;

        String desc = account.getWalletDesc_deencredit();

        View accountView = View.inflate(getContext(), R.layout.item_account_fr_deencredit, null);
        ImageView logoIv = accountView.findViewById(R.id.iaf_iv_logo);
        TextView channelTv = accountView.findViewById(R.id.iaf_tv_channel);
        TextView mobileTv = accountView.findViewById(R.id.iaf_tv_mobile);
        ImageButton deleteIb = accountView.findViewById(R.id.iaf_ib_delete);

        deleteIb.setVisibility(View.GONE);

        mobileTv.setText(account.getWalletMobile_deencredit());
        channelTv.setText(desc);

        groupLayout_deencredit.addView(accountView);

        //获取图标
        if (AppShowCustomize_deencredit.getInstance_deencredit().getChannelLogoByDesc_deencredit(desc) == null){
            new Thread(() -> {
                try {
                    if (TextUtils.isEmpty(account.getWalletLogo_deencredit())) return;
                    if (!AppShowCustomize_deencredit.getInstance_deencredit().hasAccountChannel_deencredit()) return;
                    URL url = new URL(account.getWalletLogo_deencredit());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        AppShowCustomize_deencredit.getInstance_deencredit().setChannelLogoByDesc_deencredit(desc,bitmap);
                        new Handler(Looper.getMainLooper()).post(() -> logoIv.setImageBitmap(bitmap));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }else logoIv.setImageBitmap(AppShowCustomize_deencredit.getInstance_deencredit().getChannelLogoByDesc_deencredit(desc));

    }

}
